package com.yznote.cloudesk.user.controller;

import com.yznote.cloudesk.common.Anno.CollectLogs;
import com.yznote.cloudesk.common.constants.ModuleConstant;
import com.yznote.cloudesk.common.constants.YConstant;
import com.yznote.cloudesk.common.pojo.Result;
import com.yznote.cloudesk.common.pojo.User;
import com.yznote.cloudesk.cache.yCache.YCache;
import com.yznote.cloudesk.user.server.LoginServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.UnsupportedEncodingException;

/**
 * @author : xiaozhoutongxue
 * @date : 2022/10/15 11:26
 * 登录和注册
 * 1.账号密码登录
 * 2.微信登录
 * 3.电话号码登录
 */
@RestController
@Slf4j
@Api(tags = "登录注册、未拦截")
@RequestMapping("/setup")
@CollectLogs(moduleName = ModuleConstant.USER_MODULE,needParam = false,needResponse = false)
@CrossOrigin
public class LoginController {
    @Autowired
    LoginServer loginServer;
    @Autowired
    YCache yCache;

    @ApiOperation("登录")
    @PostMapping("/login")

    public Result login(@RequestBody User user) {
        String mes = loginServer.login(user);
        if (mes==YConstant.ERROR_USERORPWD || mes ==YConstant.ERROR_STATUSEXC) {
            return Result.build(null,500,mes);
        }
        return Result.success(mes);
    }


    @ApiOperation("注册账户")
    @PostMapping("/register")
    public Result register(@RequestBody User user) throws UnsupportedEncodingException {
        String msg = loginServer.register(user);
        if (msg!=YConstant.SUCCESS){
            return Result.build(null, 500, msg);
        }
        return Result.success(null);
    }


    @ApiOperation("更改密码")
    @GetMapping("/updataUser")
    public Result updataUser(@RequestBody User user) {
        //loginServer.updataUser(user);
        return null;
    }
}
